package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.domain.ProductUnit;
import com.spzx.product.service.ProductUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


@Tag(name = "商品接口管理")
@RestController
@RequestMapping("/ProductUnit")
public class ProductUnitController extends BaseController {

    @Autowired
    private ProductUnitService productUnitService;

    //分页
    @Operation(summary = "分页条件查询接口")
    @GetMapping("/list")
    public TableDataInfo list(
            @Parameter(name = "pageNum", description = "当前页码", required = true)
            @RequestParam(value = "pageNum",defaultValue = "0",required = true) Integer pageNum,

            @Parameter(name = "pageSize", description = "每页记录数", required = true)
            @RequestParam(value = "pageSize",defaultValue = "10",required = true) Integer pageSize,

            @Parameter(name = "productUnit", description = "条件", required = false)
            ProductUnit productUnit) {
        //创建page对象
        Page<ProductUnit> pageParam = new Page<>(pageNum,pageSize);

        //封装条件
        LambdaQueryWrapper<ProductUnit> wrapper = new LambdaQueryWrapper<>();
        //非空判断
        if (StringUtils.hasText(productUnit.getName())){
        wrapper.eq(ProductUnit::getName,productUnit.getName());
        }
        //调用Service方法实现条件分页查询
        IPage<ProductUnit> pageModel = productUnitService.page(pageParam, wrapper);
        return getDataTable(pageModel);
    }
    //根据Id查询接口
    @Operation(summary = "根据Id查询")
    @GetMapping("/get{id}")
    public AjaxResult get(@PathVariable long id){
        ProductUnit byId = productUnitService.getById(id);
        return success(byId);
    }

    //添加
    @Operation(summary = "添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProductUnit productUnit){
        boolean b = productUnitService.saveOrUpdate(productUnit);
        int a = 0;
        if (b){
            a = 1;
        }
        return toAjax(a);
    }

    //修改
    @Operation(summary = "修改")
    @PutMapping("/updata")
    public AjaxResult updata(@RequestBody ProductUnit productUnit){
        boolean updateById = productUnitService.updateById(productUnit);
        int rows = 0;
        if (updateById){
            rows = 1;
        }
        return toAjax(rows);
    }

    //批量删除商品单位
    @Operation(summary = "删除商品单位 ")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        List<Long> list = Arrays.asList(ids);
        return toAjax(productUnitService.removeBatchByIds(list));
    }
    @Operation(summary = "获取全部单位")
    @GetMapping("getUnitAll")
    public AjaxResult selectProductUnitAll(){
        return success(productUnitService.list());
    }
}
